package com.tanggh.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.tanggh.entity.City;
import com.tanggh.entity.JobCategory;
import com.tanggh.entity.LocationTemp;

/**
 *@author tanggh
 */
public class TemplateUtils {

	private static final String TEMPLATE_DIR = Tools.getWebRoot()+"/template/";
	private static final String LOCATION_FTL = "location.ftl";
	private static final String LOCATION_HTML_NAME = "location.html";
	
	private static final String JOBCATEGORY_FTL = "jobCategory.ftl";
	private static final String JOBCATEGORY_HTML_NAME = "jobCategory.html";

	private static final String PUBLISH_JOBCATEGORY_FTL = "publish_jobCategory.ftl";
	private static final String PUBLISH_JOBCATEGORY_HTML_NAME = "publish_jobCategory2.jsp";
	
	public static void createLoction(List<City> list) {
		
		List<LocationTemp> tempList = new ArrayList<LocationTemp>();
		for(City c : list) {
			String code = c.getCode();
			if(code.endsWith("0000")) { //省份是以"0000"结尾的
				LocationTemp location = new LocationTemp();
				location.setName(c.getName());
				location.setUrl(Config.Site.HTTP_DOMAIN+"/"+c.getPinyin());
				tempList.add(location);
			}
		}
		
		Map<Object, Object> data = new HashMap<Object, Object>();
		data.put("locations", tempList);
		FreemarkerUtils.out(data, LOCATION_FTL, TEMPLATE_DIR+LOCATION_HTML_NAME);
	}
	
	public static void createJobCategory(List<JobCategory> list) { //生成首页分类
		Map<String, List<JobCategory>> map = new LinkedHashMap<String, List<JobCategory>>();
		for(JobCategory j : list) {
			String code = j.getCode();
			String prefix = code.substring(0,2);
			List<JobCategory> jList = map.get(prefix); 
			if(null == jList) {
				jList = new ArrayList<JobCategory>();
				map.put(prefix, jList);
			}
			jList.add(j);
		}
		
		for(String key: map.keySet()) {
			List<JobCategory> myList = map.get(key);
			Map<Object, Object> data = new HashMap<Object, Object>();
			data.put("jobCategorys", myList);
			FreemarkerUtils.out(data, JOBCATEGORY_FTL, TEMPLATE_DIR+key+"_"+JOBCATEGORY_HTML_NAME);
		}
		
//		Map<Object, Object> data = new HashMap<Object, Object>();
//		data.put("jobCategorys", map);
//		FreemarkerUtils.out(data, JOBCATEGORY_FTL, TEMPLATE_DIR+JOBCATEGORY_HTML_NAME);
	}
	
	public static void createPublishJobCategory(List<JobCategory> list) { //生成发布信息行业分类
		Map<String, List<JobCategory>> map = new LinkedHashMap<String, List<JobCategory>>();
		for(JobCategory j : list) {
			String code = j.getCode();
			String prefix = code.substring(0,2);
			List<JobCategory> jList = map.get(prefix); 
			if(null == jList) {
				jList = new ArrayList<JobCategory>();
				map.put(prefix, jList);
			}
			jList.add(j);
		}
		
		Map<Object, Object> data = new HashMap<Object, Object>();
		data.put("jobCategorys", map);
		FreemarkerUtils.out(data, PUBLISH_JOBCATEGORY_FTL, TEMPLATE_DIR+PUBLISH_JOBCATEGORY_HTML_NAME);
	}
	
	
}
